package com.android.waterdetection.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.android.waterdetection.R;
import com.android.waterdetection.bean.DeviceData;

import java.util.List;

public class DeviceDataAdapter extends RecyclerView.Adapter<DeviceDataAdapter.wDeviceDataViewHolder> {

    private List<DeviceData> mDeviceData;

    public DeviceDataAdapter(List<DeviceData> deviceDataList) {
        mDeviceData = deviceDataList;
    }

    @NonNull
    @Override
    public wDeviceDataViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_device_data,parent,false);
        return new wDeviceDataViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull wDeviceDataViewHolder holder, int position) {
        DeviceData deviceData = mDeviceData.get(position);
        holder.mTvId.setText(deviceData.getId() + "");
        holder.mTvTemp.setText(deviceData.getTemp() + "");
        holder.mTvPh.setText(deviceData.getPh() + "");
        holder.mTvTime.setText(deviceData.getDateTime());
    }

    @Override
    public int getItemCount() {
        return mDeviceData.size();
    }

    public class wDeviceDataViewHolder extends RecyclerView.ViewHolder {

        private final TextView mTvId;
        private final TextView mTvTemp;
        private final TextView mTvPh;
        private final TextView mTvTime;

        public wDeviceDataViewHolder(@NonNull View itemView) {
            super(itemView);

            mTvId = itemView.findViewById(R.id.tv_id);
            mTvTemp = itemView.findViewById(R.id.tv_temp);
            mTvPh = itemView.findViewById(R.id.tv_ph);
            mTvTime = itemView.findViewById(R.id.tv_time);
        }
    }
}
